
Refresh.Utility = {
	ensureTheme: function(theme) {
		var path = Refresh.path + 'themes/' + theme + '/style.css';
		var found = this.checkForCss(path);
		if (!found)
			this.addStyleSheet(path);			
	},
	
	ensureFloatie: function(theme) {
		var path = Refresh.path + 'themes/' + theme + '/style-floatie.css';
		var found = this.checkForCss(path);
		if (!found)
			this.addStyleSheet(path);		
	},
	
	checkForCss: function (path) {
		var styles = document.getElementsByTagName('link');
		var foundCss = false;
		for (var i=0; i<styles.length; i++) {
			var style = styles[i];
			if (style.getAttribute('href').indexOf(path) > -1) {
				foundCss = true;
				break;
			}
		}
	},
	
	addStyleSheet: function(path) {
		
		var head = document.getElementsByTagName('head')[0];
		var link  = Refresh.Web.HTML.append(head, 'link', {rel: 'stylesheet', href: path} );	
	},
	
	ensureLanguage: function(lang) {
		var path = Refresh.path + 'languages/' + lang + '.js';
		var found = this.checkForScript(path);
		if (!found)
			this.addScript(path);
	},
	
	checkForScript: function (path) {
		
		
		var scripts = document.getElementsByTagName('script');
		var foundScript = false;
		for (var i=0; i<scripts.length; i++) {
			var scriptTag = scripts[i];
			if (scriptTag.src.indexOf(path) > -1) {
				foundScript = true;
				break;
			}
		}
		return foundScript;		
	},	
	
	addScript: function(path) {
		
		var head = document.getElementsByTagName('head')[0];
		var link  = Refresh.Web.HTML.append(head, 'script', {type: 'text/javascript', src: path, charset: 'utf-8'} );		
	},
	
	makeUnselectable: function(node) {
		
		if (node && node.nodeType == 1) {
			
			node.unselectable = 'on';
			
			if (Refresh.Web.BrowserInfo.IsGecko) {
				node.style.mozUserFocus='ignore';
				node.style.mozUserInput='disabled';
				node.style.mozUserSelect='none';			
			}
			
			if (!node.childNodes)
				return;
				
			for (var i=0; i<node.childNodes.length; i++) 
				Refresh.Utility.makeUnselectable(node.childNodes[i]);			
		}
	},
	
	getNodeWordCount: function(node) {
		if (node == null) return 0;
		
		var words = '';
			
		
		// inner method
		function getNodeText(node) {
			if (node.nodeType == 3) {
				words += node.nodeValue + ' ';
			}
			if (node.childNodes != null) {
				for ( var i = 0; i < node.childNodes.length; i++) {
					getNodeText(node.childNodes.item(i));
				}
			}
		}
				
		getNodeText(node);
		

		return this.getTextWordCount(words);	
	},

	getTextWordCount: function(words) {
		var count = 0;
		
		if (!words)
			return 0;

		if (words.text)
			words = words.text;
			
		words = new String(words);
		
		var matches = words.match(/\b\w+\b/g);
		if (matches != null)
			count = matches.length;
			
		return count;
	},
	
	makeFontSizesOptions: function(sizes) {
		var options = [];
		for (var i=0; i<sizes.length; i++) {
			options.push(new Refresh.Web.UI.RichOption('<font size="' + sizes[i] + '">' + sizes[i] + '</font>',sizes[i],sizes[i]));
		}
		return options;
	},
	
	makeFontFacesOptions: function(fontnames) {
		var options = [];
		for (var i=0; i<fontnames.length; i++) {
			options.push(new Refresh.Web.UI.RichOption('<span style="font-family:' + fontnames[i] + ';">' + fontnames[i] + '</span>',fontnames[i],fontnames[i]));
		}
		return options;
	},
	
	decToHex: function(dec) {
		return parseInt(dec).toString(16); 
	},
	
	rgbToHex: function(r,g,b) {
		return "#" + this.intToHex(r) + this.intToHex(g) + this.intToHex(b);
	},
	
	intToHexColor: function( intColor ) {
		if (!intColor) 
			return null;
		
		intColor = intColor.toString(16).toUpperCase();
		while (intColor.length < 6) 
			intColor = "0" + intColor;
		
		return "#" + intColor.substring(4,6) + intColor.substring(2,4) + intColor.substring(0,2);
	},
	
	rgbStringToHex: function(rgbString) {
		
		var r, g, b;
		rgbString = rgbString.toString().toLowerCase();	
		if (rgbString.indexOf("rgb(") == -1 || rgbString.indexOf(")") == -1) 
			return rgbString;
		
		rgbString = rgbString.substring(rgbString.indexOf("(")+1, rgbString.indexOf(")")-1);
		rgb = rgbString.split(',');
		r = rgb[0];
		g = rgb[1];
		if (rgb.length == 2) 
			b = rbg[2]; 
		else 
			b = 0;	
			
		return this.rgbToHex(r,g,b);
	},
	
	intToHex: function (dec) { 
		var result = (parseInt(dec).toString(16)); 
		if (result.length ==1) 
			result  ("0" +result); 

		return result.toUpperCase(); 
	},
	
	setOpacity: function(node, opacity) {
	    var s = node.style;
	    s.opacity = (opacity / 100);
	    s.MozOpacity = (opacity / 100);
	    s.KhtmlOpacity = (opacity / 100);
	    s.filter = "alpha(opacity=" + opacity + ")";	
	},
	
	getQueryString: function() {
		var vars = [];
		
		var url = window.location.href;
		
		if (url.indexOf('?') == -1)
			return vars;
				
		// remove bookmark
		if (url.indexOf('#') > -1)
			url = url.substring(0, url.indexOf('#')-1);
			
		var queryString = url.substring(url.indexOf('?')+1);
		
		var parts = queryString.split('&');
		for (var i=0; i<parts.length; i++) {
			var c = parts[i].split('=');
			vars[c[0]] = c[1];
		}
		
		return vars;
		
	}	
};
